package org.springframework.license.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.license.service.AbstractComputerService;
import org.springframework.license.service.LicenseCreator;
import org.springframework.license.vo.Computer;
import org.springframework.license.vo.LicenseCreatorParam;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title LicenseController
 * @Author zhijun.Guo
 * @Email tiankong061015@163.com
 * @Time 2024/9/3 17:12
 * @Description
 */
//@RestController
//@RequestMapping("/license")
public class LicenseController {

//    @Value(value = "${license.license-path}")
    private String licensePath;

//    @GetMapping("/computer/information")
    public ResponseEntity<Map<String, Object>> computerInformation() {
        Computer computer = AbstractComputerService.generateComputerInformation();
        Map<String,Object> result = new HashMap<>();
        result.put("data", computer);
        result.put("code", 200);
        result.put("message", "success");
        return ResponseEntity.ok(result);
    }

//    @PostMapping("/generate/certificate")
    public ResponseEntity<Map<String, Object>> generateCertificate(@RequestBody LicenseCreatorParam param) {
        Map<String,Object> result = new HashMap<>();
        if(StringUtils.isEmpty(param.getLicensePath())) {
            param.setLicensePath(this.licensePath);
        }

        LicenseCreator licenseCreator = new LicenseCreator(param);
        boolean o = licenseCreator.generateLicense();
        if(o) {
            result.put("code", 200);
            result.put("data", licenseCreator);
            result.put("message", "success");
            return ResponseEntity.ok(result);
        }else{
            result.put("code", 500);
            result.put("message", "生成证书失败!");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

}
